﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DoeObjects;
using DoeLibsMVC.Models;
using DoeLibsMVC.Security;
using DoeLibsMVC.Filter;

namespace DoeLibsMVC.Controllers.api
{
    public class LoanableLocationCategoryController : BaseApiController
    {
        /// <summary>
        /// returns all locationcategories from the current logged in user
        /// </summary>
        /// <returns></returns>
        [Security.BasicAuthorize(Roles = Security.UserCategoryHelper.STAFF_CATEGORY)]
        public HttpResponseMessage Get()
        {
            List<SlimLocationCategory> cats = SlimLocationCategory.getAllFromUser(User.UserId);
            return Request.CreateResponse<IEnumerable<SlimLocationCategory>>(HttpStatusCode.OK, cats);


            //List<LoanableLocationCategoryModel> cats = new List<LoanableLocationCategoryModel>();

            //foreach (LoanableCategory cat in LoanableCategory.getAllFromUser(User.UserId))
            //{
            //    LoanableLocationCategoryModel llcat = new LoanableLocationCategoryModel();
            //    llcat.LocationCategory = cat;
            //    llcat.Loanables = Loanable.getByCategory(cat.CategoryId);

            //    cats.Add(llcat);
            //}

            //return Request.CreateResponse<IEnumerable<LoanableLocationCategoryModel>>(HttpStatusCode.OK, cats);
        }

        /// <summary>
        /// searches in all loanablecategories
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage Get(string q)
        {
            List<string> catNames = LoanableCategory.getAllNamesDistinct(q);

            return Request.CreateResponse<IEnumerable<string>>(HttpStatusCode.OK, catNames);
        }

        /// <summary>
        /// returns information about a locationcategory
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage Get(int id)
        {
			Models.LoanableLocationCategoryModel model = new LoanableLocationCategoryModel();
            model.LocationCategory = LoanableCategory.getById(id);
            if (model.LocationCategory != null)
            {
                model.Loanables = SlimLoanable.getByCategory(id);
                return Request.CreateResponse<Models.LoanableLocationCategoryModel>(HttpStatusCode.OK, model);

                //LoanableLocationCategoryModel llcat = new LoanableLocationCategoryModel();
                //llcat.LocationCategory = cat;
                //llcat.Loanables = Loanable.getByCategory(cat.CategoryId);

                //return Request.CreateResponse<LoanableLocationCategoryModel>(HttpStatusCode.OK, llcat);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }

        /// <summary>
        /// edit
        /// </summary>
        /// <param name="model"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [Security.BasicAuthorize(Roles = Security.UserCategoryHelper.STAFF_CATEGORY)]
        public HttpResponseMessage Put(int id, EditLoanableLocationCategoryModel model)
        {
            LoanableCategory cat = LoanableCategory.getById(id);
            if (cat != null)
            {
                if (cat.Owner.UserId == User.UserId)
                {
                    cat.Name = model.Name;
                    if (cat.save())
                    {
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }
                    else
                    {
                        return Request.CreateResponse(HttpStatusCode.InternalServerError);
                    }
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }
    }
}
